package com.dingding.special.order.cancel.biz.impl;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.model.push.PushModel;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facade.queue.dto.push.PushRequestDTO;
import com.dingding.facade.queue.enums.ClientType;
import com.dingding.facade.queue.enums.PushType;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.facade.special.ISpecialOrderFacade;
import com.dingding.order.dto.publish.PushData;
import com.dingding.special.order.aboard.code.ConfirmAboardCodeMsg;
import com.dingding.special.order.cancel.biz.ICancelSpecialOrderService;
import com.dingding.special.order.cancel.dao.ICancelSpecialOrderDao;
import com.dingding.special.order.cancel.dto.CancelSpecialOrderNoticeDTO;
import com.dingding.special.order.cancel.dto.ReqCancelSpecialOrderDTO;
import com.dingding.special.order.cancel.dto.ResCancelSpecialOrderDTO;

/**
 * 
 * @ClassName: CancelSpecialOrderServiceImpl 
 * @Description: 取消专车订单Service实现类
 * @author YHQ
 * @date 2016年8月21日 下午7:43:43
 * @since V2.4.0
 * 
 */
@Component("cancelSpecialOrderService")
public class CancelSpecialOrderServiceImpl implements ICancelSpecialOrderService {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "cancelSpecialOrderDao")
	private ICancelSpecialOrderDao cancelSpecialOrdertDao;

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	@Resource(name = "queueService")
	private IQueueFacade queueFacade;

	@Resource(name = "specialOrderFacade")
	private ISpecialOrderFacade specialOrderFacade;

	/**
	 * 
	 * @Title: cancelSpecialOrder 
	 * @Description: 取消专车订单
	 * @param reqCancelSpecialOrderDTO  取消专车订单请求Bean
	 * @author YHQ 
	 * @since V2.4.0
	 * @return ResponseEntity<ResCancelSpecialOrderDTO>    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public ResponseEntity<ResCancelSpecialOrderDTO> cancelSpecialOrder(
			ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO) {

		// 取消专车订单响应Bean
		ResponseEntity<ResCancelSpecialOrderDTO> response = new ResponseEntity<ResCancelSpecialOrderDTO>();

		// 验证请求参数是否为空
		boolean flag = this.validateParam(response, reqCancelSpecialOrderDTO);

		if (!flag) {
			return response;
		}

		// 取消订单操作
		boolean cancelOrder = this.cancelSpecialOrderOperation(response, reqCancelSpecialOrderDTO);
		
		// 司机取消订单成功，推送消息给乘客
		if(cancelOrder){
			
			this.cancelSpecialOrderNotice(reqCancelSpecialOrderDTO);
		}
		
		response.setCode(HttpStatus.SC_OK);
		return response;
	}

	/**
	 * 
	 * @Title: validateParam 
	 * @Description: 验证请求参数 
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.4.0
	 * @return boolean    返回类型
	 * 
	 */
	public boolean validateParam(ResponseEntity<ResCancelSpecialOrderDTO> response,
			ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO) {

		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(reqCancelSpecialOrderDTO,
				ReqCancelSpecialOrderDTO.class);

		if (!valiResult.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(ConfirmAboardCodeMsg.DATA_NULL_PARAM_MESSAGE);// 客户端请求数据为空
			response.setPromptMessage(ConfirmAboardCodeMsg.APP_ERROR);// 客户端请求数据为空
			response.setNullProperties(valiResult.getNullFieldName());
			log.error("为空字段为：" + valiResult.getNullFieldName());
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: cancelSpecialOrderOperation 
	 * @Description: 取消专车订单操作 
	 * @param response 取消专车订单响应Bean
	 * @param reqCancelSpecialOrderDTO 取消专车订单请求Bean
	 * @author YHQ 
	 * @since V2.4.0 
	 * @return boolean    返回类型
	 * 
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public boolean cancelSpecialOrderOperation(ResponseEntity<ResCancelSpecialOrderDTO> response,
			ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO) {

		UserspecialorderT userspecialorderT = this.cancelSpecialOrdertDao.operateOldOrderT(response, reqCancelSpecialOrderDTO);
		
		if(userspecialorderT == null){
			return false;
		}

		// 将该订单加入队列
		this.queueFacade.addSpecialOrderToQueue(userspecialorderT);

		return true;
	}


	/**
	 * 
	 * @Title: getUserSpecialOrder 
	 * @Description: 将专车临时订单内容转到专车正式订单 
	 * @param userspecialorderT 专车临时订单对象
	 * @author YHQ 
	 * @since V2.4.0
	 * @return Userspecialorder    返回类型
	 * 
	 */
	public Userspecialorder getUserSpecialOrder(UserspecialorderT userspecialorderT) {

		// 专车订单正式表
		Userspecialorder userspecialorder = new Userspecialorder();
		BeanUtils.copyProperties(userspecialorderT, userspecialorder);
		return userspecialorder;
	}

	/**
	 * 
	 * @Title: confirmAboardNotice 
	 * @Description: 确认上车通知操作
	 * @param reqConfirmAboardDTO 确认上车客户端请求参数
	 * @author YHQ 
	 * @since V2.4.0
	 * @return void    返回类型
	 * 
	 */
	public void cancelSpecialOrderNotice(ReqCancelSpecialOrderDTO reqCancelSpecialOrderDTO) {

		// 取消订单通知DTO
		CancelSpecialOrderNoticeDTO cancelNotice = cancelSpecialOrdertDao
				.getNoticeInfo(reqCancelSpecialOrderDTO.getOid(), reqCancelSpecialOrderDTO.getCancelType());

		if (cancelNotice != null) {

			// 推送消息
			this.confirmAboardPushMessage(cancelNotice);

			// 司机确认上车-通知推送
			this.confirmAboardPushNotice(cancelNotice);
		}

	}

	/**
	 * 
	 * @Title: confirmAboardPushMessage 
	 * @Description: 确认上车后推送消息
	 * @param confirmAboardNoticeDTO  确认上车，客户端请求参数
	 * @author YHQ 
	 * @since V2.4.0
	 * @return void    返回类型
	 * 
	 */
	public void confirmAboardPushMessage(CancelSpecialOrderNoticeDTO cancelNotice) {

		// 推送Bean
		PushData pushData = new PushData();

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("passengerCancelSpecialOrderPush");
		pushData.setResponseBody(cancelNotice);

		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(cancelNotice.getAliasId());
		pushRequestDTO.setAlert("司机取消订单，系统重新指派，请等待");

		// 设备类型
		if (cancelNotice.getClientType().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (cancelNotice.getClientType().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.alert_msgcontent);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData));

		log.info("订单取消推送内容： " + JSONUtil.toJSONString(pushRequestDTO));
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
	}

	/**
	 * 
	 * @Title: confirmAboardPushNotice 
	 * @Description: 确认上车后，通知推送消息
	 * @param confirmAboardNoticeDTO 确认上车，客户端请求参数
	 * @author YHQ 
	 * @since V2.4.0
	 * @return void    返回类型
	 * 
	 */
	public void confirmAboardPushNotice(CancelSpecialOrderNoticeDTO cancelNotice) {

		// 推送Bean
		PushData pushData = new PushData();

		String content = "专车司机取消了订单，抱歉！";

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("newsNoticePush");
		// 确认到达推送内容
		pushData.setResponseBody(PushModel.noticeModel(1, content, null));

		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(cancelNotice.getAliasId());

		// 设备类型
		if (cancelNotice.getClientType().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (cancelNotice.getClientType().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.msgcontent);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData));

		log.info("订单取消通知内容： " + JSONUtil.toJSONString(pushRequestDTO));
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
	}

}
